package com.cbtx.networklib.config

import androidx.collection.ArrayMap

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/6/25 4:59 PM
 * desc   :
 */
class HostConfig private constructor(private val hostEntryMap: Map<String, HostEntry>) {

    enum class Env {
        PROD, QA, DEV
    }

    data class HostEntry(val prod: String, val qa: String, val dev: String)

    fun getHost(name: String, env: Env): String? {
        val hostEntry = hostEntryMap[name]
        return if (hostEntry != null) {
            when (env) {
                Env.DEV -> hostEntry.dev
                Env.QA -> hostEntry.qa
                else -> hostEntry.prod
            }
        } else {
            null
        }
    }

    class Builder {
        private val hostEntryMap: MutableMap<String, HostEntry> = ArrayMap()

        fun addHostEntry(alias: String, hostEntry: HostEntry): Builder {
            hostEntryMap[alias] = hostEntry
            return this
        }

        fun build(): HostConfig {
            return HostConfig(hostEntryMap)
        }
    }

}
